package org.kl.bf.entity.basic;

import java.sql.Timestamp;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.hibernate.validator.constraints.NotBlank;
import org.kl.bf.entity.AuditableEntity;
import org.kl.bf.entity.Excel;
import org.kl.auditsupervision.entity.fnd.Employee;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
@Table(name = "basic_user")
public class User extends AuditableEntity {
	private static final long serialVersionUID = 1L;
	@Excel(exportName = "登录账号")
	private String loginNo;
	@Excel(exportName = "登录姓名")
	private String trueName;
	@Excel(exportName = "手机号")
	private String phone;
	private String plainPassword;
	private String password;
	private String salt;
	@Excel(exportName = "用户类型")
	private String userType;
	private Integer sysManager;
	private Integer timeFlag;
	private Set<DataRole> dataRoleSet;
	private Set<Role> roleSet;
	private Set<Menu> favMenus;
	/*
	 * @Excel(exportName = "工号|岗位|所属组织|工作电话|电话号码|短号", exportProperty =
	 * "jobNumber|post.postName|organization.orgName|workPhone|phoneNumber|shortNumber"
	 * ) private Staffinfo staffinfo; // 关联员工
	 */@Excel(exportName = "员工姓名", exportProperty = "trueName")
	private Employee employee;

	/**
	 * 邮箱验证用到的字段
	 */
	private String email; // 邮箱
	private Integer status = 0; // 激活状态
	private String validateCode; // 验证码
	private Timestamp validateTime; // 验证时间
	private Timestamp overdueTime; // 过期时间

	@OneToOne
	@JoinColumn(name = "emp_id")
	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	/*
	 * @OneToOne
	 * 
	 * @JoinColumn(name = "staffinfo_id") public Staffinfo getStaffinfo() {
	 * return staffinfo; }
	 * 
	 * public void setStaffinfo(Staffinfo staffinfo) { this.staffinfo =
	 * staffinfo; }
	 */

	public Integer getSysManager() {
		return sysManager;
	}

	public void setSysManager(Integer sysManager) {
		this.sysManager = sysManager;
	}

	@JsonIgnore
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "basic_fav_menu", schema = "", joinColumns = { @JoinColumn(name = "user_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "menu_id", nullable = false, updatable = false) })
	public Set<Menu> getFavMenus() {
		return favMenus;
	}

	public void setFavMenus(Set<Menu> favMenus) {
		this.favMenus = favMenus;
	}

	public User() {
	}

	public User(String id) {
		this.id = id;
	}

	@JsonIgnore
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "basic_user_data_role", schema = "", joinColumns = { @JoinColumn(name = "user_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "data_role_id", nullable = false, updatable = false) })
	public Set<DataRole> getDataRoleSet() {
		return dataRoleSet;
	}

	public void setDataRoleSet(Set<DataRole> dataRoleSet) {
		this.dataRoleSet = dataRoleSet;
	}

	@JsonIgnore
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "basic_user_role", schema = "", joinColumns = { @JoinColumn(name = "user_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "role_id", nullable = false, updatable = false) })
	public Set<Role> getRoleSet() {
		return roleSet;
	}

	public void setRoleSet(Set<Role> roleSet) {
		this.roleSet = roleSet;
	}

	@NotBlank
	public String getLoginNo() {
		return loginNo;
	}

	public void setLoginNo(String loginNo) {
		this.loginNo = loginNo;
	}

	public String getTrueName() {
		return trueName;
	}

	public void setTrueName(String trueName) {
		this.trueName = trueName;
	}

	
	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	// 不持久化到数据库，也不显示在Restful接口的属性.
	@Transient
	@JsonIgnore
	public String getPlainPassword() {
		return plainPassword;
	}

	public void setPlainPassword(String plainPassword) {
		this.plainPassword = plainPassword;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@JsonIgnore
	public String getSalt() {
		return salt;
	}

	public void setSalt(String salt) {
		this.salt = salt;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getValidateCode() {
		return validateCode;
	}

	public void setValidateCode(String validateCode) {
		this.validateCode = validateCode;
	}

	public Integer getTimeFlag() {
		return timeFlag;
	}

	public void setTimeFlag(Integer timeFlag) {
		this.timeFlag = timeFlag;
	}

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08:00")
	public Timestamp getValidateTime() {
		return validateTime;
	}

	public void setValidateTime(Timestamp validateTime) {
		this.validateTime = validateTime;
	}

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08:00")
	public Timestamp getOverdueTime() {
		return overdueTime;
	}

	public void setOverdueTime(Timestamp overdueTime) {
		this.overdueTime = overdueTime;
	}

}